package com.andy.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.andy.dao.IExamSubjectDAO;
import com.andy.dao.IExamTestDAO;
import com.andy.dao.IExamTestTypeDAO;
import com.andy.entity.ExamSubject;
import com.andy.entity.ExamTest;
import com.andy.entity.ExamTestType;
import com.andy.vo.ExamSubjectVO;
import com.andy.vo.ExamTestTypeVO;
import com.andy.vo.ExamTestVO;

@Service
@Transactional
public class ManageTestService implements IManageTestService {
	
	@Autowired
	private IExamTestDAO examTestDAO;
	
	@Autowired
	private IExamTestTypeDAO examTestTypeDAO;
	
	@Autowired
	private IExamSubjectDAO examSubjectDAO;

	@Override
	public List<ExamTestVO> loadAllTest() {
		List<ExamTest> entities = examTestDAO.loadAll();
		if (entities != null) {
			ExamTestVO vo = null;
			List<ExamTestVO> resultList = new ArrayList<ExamTestVO>();
			for (ExamTest entity : entities) {
				vo = new ExamTestVO();
				BeanUtils.copyProperties(entity, vo);
				
				// get subject
				ExamSubject subject = examSubjectDAO.get(entity.getSubId());
				if (subject != null) {
					vo.setSubName(subject.getName());
				}
				
				// convert ExamTestType to ExamTestTypeVO
				ExamTestTypeVO typeVO = new ExamTestTypeVO();
				BeanUtils.copyProperties(entity.getExamTestType(), typeVO);
				vo.setExamTestType(typeVO);
				
				resultList.add(vo);
			}
			return resultList;
		}
		return null;
	}

	@Override
	public List<ExamSubjectVO> getSubjectList() {
		List<ExamSubject> subjectList = examSubjectDAO.loadAll();
		List<ExamSubjectVO> resultList = new ArrayList<ExamSubjectVO>();
		for (ExamSubject subject : subjectList) {
			ExamSubjectVO vo = new ExamSubjectVO();
			BeanUtils.copyProperties(subject, vo);
			resultList.add(vo);
		}
		return resultList;
	}

	@Override
	public List<ExamTestTypeVO> getTestTypeList() {
		List<ExamTestType> testTypeList = examTestTypeDAO.loadAll();
		List<ExamTestTypeVO> resultList = new ArrayList<ExamTestTypeVO>();
		for (ExamTestType testType : testTypeList) {
			ExamTestTypeVO vo = new ExamTestTypeVO();
			BeanUtils.copyProperties(testType, vo);
			resultList.add(vo);
		}
		return resultList;
	}

	@Override
	public void addTest(ExamTestVO examTestVO) {
		if (examTestVO != null) {
			ExamTest entity = new ExamTest();
			BeanUtils.copyProperties(examTestVO, entity);
			
			if (examTestVO.getExamTestType() != null) {
				ExamTestType testType = examTestTypeDAO.get(examTestVO.getExamTestType().getId());
				entity.setExamTestType(testType);
			}
			examTestDAO.save(entity);
			examTestDAO.flush();
		}
	}

	@Override
	public void updateTest(ExamTestVO examTestVO) {
		if (examTestVO != null) {
			ExamTest entity = examTestDAO.get(examTestVO.getId());
			BeanUtils.copyProperties(examTestVO, entity);
			
			if (examTestVO.getExamTestType() != null) {
				ExamTestType testType = examTestTypeDAO.get(examTestVO.getExamTestType().getId());
				entity.setExamTestType(testType);
			}
			examTestDAO.update(entity);
			examTestDAO.flush();
		}
	}

	@Override
	public void deleteTest(Integer testId) {
		ExamTest entity = examTestDAO.get(testId);
		if (entity != null) {
			examTestDAO.delete(entity);
		}
	}

	@Override
	public ExamTestVO getTestById(Integer testId) {
		ExamTest entity = examTestDAO.get(testId);
		if (entity != null) {
			ExamTestVO vo = new ExamTestVO();
			BeanUtils.copyProperties(entity, vo);
			
			// get subject
			ExamSubject subject = examSubjectDAO.get(entity.getSubId());
			if (subject != null) {
				vo.setSubName(subject.getName());
			}
			
			// convert ExamTestType to ExamTestTypeVO
			ExamTestTypeVO typeVO = new ExamTestTypeVO();
			BeanUtils.copyProperties(entity.getExamTestType(), typeVO);
			vo.setExamTestType(typeVO);
			return vo;
		}
		return null;
	}

	@Override
	public void createTest() {
		// TODO Auto-generated method stub
	}
	
}
